package com.example.dao;

import com.example.pojo.Coach;
import com.example.pojo.Course;
import com.example.pojo.Member;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class LoginDao {
    //管理员登陆
    public Boolean adminLogin(String phone,String pass)
    {
        DataSource ds = DataBaseUtils.getDataSource();
        JdbcTemplate template=new JdbcTemplate(ds);
        String sql="SELECT COUNT(*) FROM admin WHERE num=? AND pass=?";
        return template.queryForObject(sql,Boolean.class, phone,pass);
    }

    //教练登录
    public Coach coachLogin(String phone, String pass)
    {
        DataSource ds = DataBaseUtils.getDataSource();
        JdbcTemplate template=new JdbcTemplate(ds);
        String sql="SELECT * FROM coach WHERE phone=? AND pass=?";
        Coach coach;
        try
        {
            coach=template.queryForObject(sql, new Object[]{phone, pass},
                    new RowMapper<Coach>() {
                        @Override
                        public Coach mapRow(ResultSet resultSet, int i) throws SQLException {
                            Coach coach=new Coach();
                            coach.setId(resultSet.getInt(1));
                            coach.setName(resultSet.getString(2));
                            coach.setAge(resultSet.getInt(3));
                            coach.setSex(resultSet.getString(4));
                            coach.setPhone(resultSet.getString(5));
                            coach.setPass(resultSet.getString(6));
                            return coach;
                        }
                    });
        }
        catch (EmptyResultDataAccessException e)
        {
            coach=null;
        }
        return coach;
    }
    //普通会员登录
    public Member memberLogin(String phone,String pass)  {
        DataSource ds = DataBaseUtils.getDataSource();
        JdbcTemplate template=new JdbcTemplate(ds);
        String sql="SELECT * FROM member WHERE phone=? AND pass=?";
        Member member;
        try{
            member=template.queryForObject(sql, new Object[]{phone,pass},new RowMapper<Member>() {
                @Override
                public Member mapRow(ResultSet resultSet, int i) throws SQLException {
                    Member member = new Member();
                    member.setId(resultSet.getInt(1));
                    member.setName(resultSet.getNString(2));
                    member.setAge(resultSet.getInt(3));
                    member.setSex(resultSet.getString(4));
                    member.setPhone(resultSet.getString(5));
                    member.setPass(resultSet.getString(6));
                    member.setLevel(resultSet.getString(7));
                    return member;
                }
            });
        }
        catch (EmptyResultDataAccessException e)
        {
            member=null;
        }
        return member;
    }
    @Test
    public void mytest()
    {
        LoginDao obj=new LoginDao();
        //System.out.println(obj.memberLogin("18812341234","123456"));
        //System.out.println(obj.memberLogin("18812341234","1234hhgh56"));
        //System.out.println(obj.adminLogin("admin","admin"));
        //System.out.println(obj.coachLogin("13012341234","123456"));
    }
}
